package com.angel.web.sys;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.angel.core.util.JsonResult;
import com.angel.core.util.JsonUtils;
import com.angel.health.BuzExamination;
import com.angel.health.ln.ChartMap;
import com.angel.provider.health.BuzAssessmentProvider;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzDoctorProvider;
import com.angel.provider.health.BuzExaminationProvider;
import com.angel.provider.health.BuzSurveyProvider;
import com.angel.provider.sys.SysManagerAreaProvider;

/**
 * 控制台啊
 * @author hasee
 *
 */

@RequestMapping("/console")
@Controller
public class SysConsoleController {

	@Autowired
	private SysManagerAreaProvider provider;
	
	@Autowired
	private BuzCustomerProvider buzCustomerProvider;
	
	@Autowired
	private BuzDoctorProvider buzDoctorProvider;
	
	@Autowired
	private BuzSurveyProvider buzSurveyProvider;
	
	@Autowired
	private BuzExaminationProvider buzExaminationProvider;
	
	
	@Autowired
	private BuzAssessmentProvider buzAssessmentProvider;
	
	/**
	 * 进入控制台
	 */
	@RequestMapping("/index")
	public String console(){
		return "console";
	}
	
	/**
	 * 初始化所有控制台
	 */
	@RequestMapping("/")
	public void init(){
		
	}
	
	/**
	 * 用户医师注册展示
	 * @param response
	 */
	@RequestMapping("/loadDoctorAndUser")
	public void loadDoctorAndUser(HttpServletResponse response){
		List<ChartMap> user = buzCustomerProvider.loadChart();
		
		List<ChartMap> doct = buzDoctorProvider.loadChart();
		Map<String,String> u = null;
		List<Map<String,String>> list = new ArrayList<Map<String, String>>();
		if(user!=null){
			for(int i=0;i<user.size();i++){
				if("1".equals(user.get(i).getName())){
					u = new HashMap<String, String>();
					u.put("name", "会员APP");
					u.put("value", user.get(i).getValue());
					list.add(u);
				}
				if("0".equals(user.get(i).getName())){
					u = new HashMap<String, String>();
					u.put("name", "会员线下");
					u.put("value", user.get(i).getValue());
					list.add(u);
				}
			}
		}
		if(doct!=null){
			for(int i=0;i<doct.size();i++){
				if("2".equals(doct.get(i).getName())){
					u = new HashMap<String, String>();
					u.put("name", "医师APP");
					u.put("value", doct.get(i).getValue());
					list.add(u);
				}
				if("0".equals(doct.get(i).getName())){
					u = new HashMap<String, String>();
					u.put("name", "医师线下");
					u.put("value", doct.get(i).getValue());
					list.add(u);
				}
			}
		}

		JsonResult.sendResult(response, JsonUtils.toJson(list));
	}
	
	/**
	 * 问卷调查
	 * @param response
	 */
	@RequestMapping("/loadReport")
	public void loadReport(HttpServletResponse response){
		List<ChartMap> report = buzSurveyProvider.loadChart();
		
		List<ChartMap> examination = buzExaminationProvider.loadChart();
		
		List<ChartMap> assessment =  buzAssessmentProvider.loadChart();
		String[] strReport = new String[7];
		if(report!=null){
			for(int i=0;i<7;i++){
				String val = "0";
				for(int k=0;k<report.size();k++){
					if(beforeDate(i).equals(report.get(k).getName())){
						val = report.get(k).getValue();
					}
				}
				strReport[(i-6)*-1] = val;
			}
		}
		
		String[] strExamination = new String[7];
		if(examination!=null){
			for(int i=0;i<7;i++){
				String val = "0";
				for(int k=0;k<examination.size();k++){
					if(beforeDate(i).equals(examination.get(k).getName().substring(0,10))){
						val = examination.get(k).getValue();
					}
				}
				strExamination[(i-6)*-1] = val;
			}
		}
		
		String[] strAssessment = new String[7];
		if(assessment!=null){
			for(int i=0;i<7;i++){
				String val = "0";
				for(int k=0;k<assessment.size();k++){
					if(beforeDate(i).equals(assessment.get(k).getName().substring(0,10))){
						val = assessment.get(k).getValue();
					}
				}
				strAssessment[(i-6)*-1] = val;
			}
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("strReport", strReport);
		map.put("strExamination", strExamination);
		map.put("strAssessment", strAssessment);
		JsonResult.sendResult(response, JsonUtils.toJson(map));
	}
	
	public static String beforeDate(int val) {
		Calendar calendar = Calendar.getInstance(); 
		calendar.add(Calendar.DAY_OF_MONTH, -val);  
		Date d = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String date = sdf.format(calendar.getTime());  
        return date;
	}
}
